package com.gezi.admin.module.sys.log;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gezi.admin.aop.log.Logging;
import com.gezi.admin.config.exception.type.ApiException;
import com.gezi.admin.module.app.user.AppUserEntity;
import com.gezi.admin.module.app.user.AppUserService;
import com.gezi.admin.module.sys.user.SysUserEntity;
import com.gezi.admin.module.sys.log.model.SysOperateLogSeachVo;
import com.gezi.admin.module.sys.log.model.SysOperateLogStateEnum;
import com.gezi.admin.module.sys.log.model.*;
import com.gezi.admin.module.config.BaseController;
import com.gezi.admin.module.config.utils.PageUtil;
import com.gezi.admin.module.config.utils.PageVo;
import com.gezi.admin.module.sys.user.SysUserService;
import com.gezi.admin.security.model.LoginClientEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Map;

/**
 * @Author 格子代码
 * @createDate 2024/2/10 09:00
 * @contact 1424529270@qq.com
 */
@Api(tags = "操作日志")
@RestController
@RequestMapping("/api/sys/log")
public class SysOperateLogController extends BaseController {

    @Autowired
    private SysOperateLogService sysOperateLogService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private AppUserService appUserService;

    /**
     *
     * 搜索
     * @return
     */
    @ApiOperation("查询分页")
    @GetMapping(value = "/findAllByPage")
    public Object findAllByPage(PageVo page, SysOperateLogSeachVo searchVo) {
        Page<Map> pageData = sysOperateLogService.findByPage(PageUtil.initMpPage(page),searchVo);
        for (Map record : pageData.getRecords()) {
            if(LoginClientEnum.admin.isSame(record.get("client").toString())){
                SysUserEntity sysUserEntity = sysUserService.getById(record.get("userId").toString());
                if(sysUserEntity != null){
                    record.put("userNickName",sysUserEntity.getNickName());
                }
            }else{
                AppUserEntity appUserEntity = appUserService.getById(record.get("userId").toString());
                if(appUserEntity != null){
                    record.put("userNickName",appUserEntity.getNickName());
                }
            }

            record.put("stateShow", SysOperateLogStateEnum.valueOf(record.get("state").toString()).getRemark());
            record.put("clientShow", LoginClientEnum.valueOf(record.get("client").toString()).getRemark());
        }
        return pageData;
    }

    /**
     * 通过id查询
     * @return
     */
    @Validated
    @GetMapping(value = "/findById/{id}")
    public Map findById(@PathVariable @NotBlank(message = "参数不能为空")String id) {
        SysOperateLogEntity appOperateLogEntity = sysOperateLogService.getById(id);
        if(appOperateLogEntity == null){
            throw ApiException.getInstance("数据不存在，请刷新重试");
        }

        Map result = JSON.parseObject(JSON.toJSONString(appOperateLogEntity),Map.class);

        result.put("stateShow", SysOperateLogStateEnum.valueOf(result.get("state").toString()).getRemark());
        result.put("clientShow", LoginClientEnum.valueOf(result.get("client").toString()).getRemark());
        return result;
    }

    @Logging(tags = "操作日志",value = "删除")
    @ApiOperation("删除")
    @DeleteMapping(value = "/delByIds/{ids}")
    public void delAllByIds(@PathVariable String[] ids) {
        sysOperateLogService.removeBatchByIds(Arrays.asList(ids));
    }
}
